package com.example.imtoken.modules.main.wallet

import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.LogUtils
import com.example.imtoken.core.App
import com.example.imtoken.core.base.BaseViewModel
import com.example.imtoken.core.http.BaseObserver
import com.example.imtoken.core.logic.network.RetrofitUtils
import com.example.imtoken.entitys.http.*
import com.example.imtoken.room.base.AppDataBase
import com.example.imtoken.room.schema.AccountSchema
import io.reactivex.schedulers.Schedulers

class WalletViewModel : BaseViewModel() {

    private val balanceLiveData = MutableLiveData("~")

    private val defAccountLiveData = MutableLiveData<AccountSchema>()

    private val coinResourceLiveData = MutableLiveData<CoinResourceEntity>()

    fun getCoinResource(): MutableLiveData<CoinResourceEntity> {
        return coinResourceLiveData
    }

    fun getDefAccountData(): MutableLiveData<AccountSchema> {
        return defAccountLiveData
    }

    fun getBalanceData(): MutableLiveData<String> {
        return balanceLiveData
    }

//    fun homeData() {
//        mDialog.postValue(true)
//        val coins = RetrofitUtils.apiService?.coins()
//        val balance = RetrofitUtils.apiService?.balance("eth")
//        val disposable = coins?.subscribeOn(Schedulers.io())
//            ?.unsubscribeOn(Schedulers.io())
//            ?.doOnNext {
//                LogUtils.e("查询货币列表", it)
//                coinsLiveData.postValue(it.data)
//            }
//            ?.observeOn(Schedulers.io())
//            ?.flatMap {
//                balance
//            }
//            ?.subscribe({
//                mDialog.postValue(false)
//                LogUtils.e("货币金额查询", it)
//                balanceLiveData.postValue(it.data)
//            }) {
//                mDialog.postValue(false)
//                LogUtils.e("请求失败", it.message)
//            }
////        disposable?.dispose()
//    }

    fun getDefAccount() {
        val appLogin = AppDataBase.instance.getAppLoginDao().getAppLogin()
        val account = AppDataBase.instance.getAccountDao().getDefAccount(appLogin.walletAddress)

        //保存初始数据到App全局
        val coin = Coin(account.address, account.coin, account.mnemonic, account.publicKey)
        val createEntity = CreateEntityItem(coin, account.token)
        App.createLiveData.value = createEntity

        defAccountLiveData.value = account

        getBalance(account.coin)
    }

    private fun getBalance(coin: String?) {
        RetrofitUtils.apiService?.balance(coin)
            ?.subscribeOn(Schedulers.io())
            ?.unsubscribeOn(Schedulers.io())
            ?.subscribe(object : BaseObserver<ResponseEntity<BalanceEntity>>() {
                override fun onSuccess(value: ResponseEntity<BalanceEntity>) {
                    LogUtils.e(value)
                    if (value.code == 0) {
                        value.data?.forEach {
                            if (it.coin == coin) {
                                balanceLiveData.postValue(it.amount.toString())
                            }
                        }
                    } else {
                        mErrorStr.postValue(value.msg)
                    }
                }

                override fun onFailed(e: Throwable) {
                }

            })
    }

    fun getCoinResource(coin: String?) {
        RetrofitUtils.apiService?.coinResource(coin)
            ?.subscribeOn(Schedulers.io())
            ?.unsubscribeOn(Schedulers.io())
            ?.subscribe(object : BaseObserver<ResponseEntity<CoinResourceEntity>>() {
                override fun onSuccess(value: ResponseEntity<CoinResourceEntity>) {
                    if (value.code == 0) {
                        coinResourceLiveData.postValue(value.data)
                    }
                }

                override fun onFailed(e: Throwable) {
                }

            })
    }

}